package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.service.ExchangeGoldDiamondsLogService;
import com.vf.admin.client.service.ExchangeGoldDiamondsService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.entity.ExchangeGoldDiamonds;
import com.vf.admin.miaodong.entity.ExchangeGoldDiamondsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 兑换
 */
@RestController
@RequestMapping("/app/exchange")
public class AppExchangeController extends ClientBaseController {

    @Autowired
    ExchangeGoldDiamondsService exchangeGoldDiamondsService;
    @Autowired
    ExchangeGoldDiamondsLogService exchangeGoldDiamondsLogService;
    @Autowired
    AppUserService appUserService;

    @RequestMapping(value = "/getList")
    public Result getList() {
        Map<String, Object> data = new HashMap<>();
        List<ExchangeGoldDiamonds> list = exchangeGoldDiamondsService.list();
        AppUser user = appUserService.get(getUserId());
        if(user != null) {
            data.put("list", list);
            data.put("diamonds", user.getDiamonds());
            data.put("goldCoin", user.getJinbi());
        }
        return sucessJSON(data);
    }

    /**
     * 金币兑换钻石
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/exchange")
    public Result exchange(Integer id) {
        return exchangeGoldDiamondsService.exchange(getUserId(), id);
    }

    /**
     * 兑换记录
     * @param page
     * @return
     */
    @RequestMapping(value = "/exchangeLog")
    public Result exchangeLog(SplitPageBean page) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", getUserId());
        List<ExchangeGoldDiamondsLog> list = exchangeGoldDiamondsLogService.page(params, page);

        return sucessPage(list, page.getNextPageNo());
    }


}
